<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>GO基础-基本数据类型 | 小狐档案库</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/favicon.ico">
    <meta name="description" content="学习资料">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.0c36f186.css" as="style"><link rel="preload" href="/assets/js/app.7029ddab.js" as="script"><link rel="preload" href="/assets/js/3.ebaa3085.js" as="script"><link rel="preload" href="/assets/js/1.8ce67e8c.js" as="script"><link rel="preload" href="/assets/js/29.4d95742d.js" as="script"><link rel="prefetch" href="/assets/js/10.15c1f9c8.js"><link rel="prefetch" href="/assets/js/11.dc2fbddf.js"><link rel="prefetch" href="/assets/js/12.26ab1f36.js"><link rel="prefetch" href="/assets/js/13.6e1d4dbc.js"><link rel="prefetch" href="/assets/js/14.0cd48e80.js"><link rel="prefetch" href="/assets/js/15.74a9f029.js"><link rel="prefetch" href="/assets/js/16.24ef3382.js"><link rel="prefetch" href="/assets/js/17.c8a9bd0f.js"><link rel="prefetch" href="/assets/js/18.fbd494c2.js"><link rel="prefetch" href="/assets/js/19.f9a0df71.js"><link rel="prefetch" href="/assets/js/20.e6d0df38.js"><link rel="prefetch" href="/assets/js/21.932645cc.js"><link rel="prefetch" href="/assets/js/22.d6027dce.js"><link rel="prefetch" href="/assets/js/23.53bce141.js"><link rel="prefetch" href="/assets/js/24.ed3b8b64.js"><link rel="prefetch" href="/assets/js/25.ed88877e.js"><link rel="prefetch" href="/assets/js/26.f5ad24d5.js"><link rel="prefetch" href="/assets/js/27.64ad45b4.js"><link rel="prefetch" href="/assets/js/28.f308f8ab.js"><link rel="prefetch" href="/assets/js/30.0a867de0.js"><link rel="prefetch" href="/assets/js/31.f12def86.js"><link rel="prefetch" href="/assets/js/32.6b4fd8b7.js"><link rel="prefetch" href="/assets/js/33.4bb2aa9c.js"><link rel="prefetch" href="/assets/js/34.013e731c.js"><link rel="prefetch" href="/assets/js/35.6b263ef8.js"><link rel="prefetch" href="/assets/js/36.06acf3cc.js"><link rel="prefetch" href="/assets/js/37.fe72c29c.js"><link rel="prefetch" href="/assets/js/38.9b4e0bd6.js"><link rel="prefetch" href="/assets/js/39.0cde0408.js"><link rel="prefetch" href="/assets/js/4.9b5650bb.js"><link rel="prefetch" href="/assets/js/40.56bda9a8.js"><link rel="prefetch" href="/assets/js/41.007ae34c.js"><link rel="prefetch" href="/assets/js/42.bdaf414b.js"><link rel="prefetch" href="/assets/js/43.78d32d4e.js"><link rel="prefetch" href="/assets/js/44.a10cf8a5.js"><link rel="prefetch" href="/assets/js/45.19e14721.js"><link rel="prefetch" href="/assets/js/46.57ebcf06.js"><link rel="prefetch" href="/assets/js/47.9a513ffe.js"><link rel="prefetch" href="/assets/js/48.245b165a.js"><link rel="prefetch" href="/assets/js/49.fab43655.js"><link rel="prefetch" href="/assets/js/5.a16ef7f8.js"><link rel="prefetch" href="/assets/js/50.48c191fd.js"><link rel="prefetch" href="/assets/js/51.28a94b0e.js"><link rel="prefetch" href="/assets/js/52.e783fbf9.js"><link rel="prefetch" href="/assets/js/53.2f0e960e.js"><link rel="prefetch" href="/assets/js/54.577ff54e.js"><link rel="prefetch" href="/assets/js/55.4bb99598.js"><link rel="prefetch" href="/assets/js/56.e820e954.js"><link rel="prefetch" href="/assets/js/57.0cea0cfa.js"><link rel="prefetch" href="/assets/js/58.e31cfcd0.js"><link rel="prefetch" href="/assets/js/59.458ab168.js"><link rel="prefetch" href="/assets/js/6.7da424ba.js"><link rel="prefetch" href="/assets/js/60.e9caace2.js"><link rel="prefetch" href="/assets/js/61.0b61afdb.js"><link rel="prefetch" href="/assets/js/62.c240acab.js"><link rel="prefetch" href="/assets/js/63.71cd2a97.js"><link rel="prefetch" href="/assets/js/64.977ea29b.js"><link rel="prefetch" href="/assets/js/65.79b2d62a.js"><link rel="prefetch" href="/assets/js/66.8da69071.js"><link rel="prefetch" href="/assets/js/67.fdc57d7e.js"><link rel="prefetch" href="/assets/js/68.d43eabe9.js"><link rel="prefetch" href="/assets/js/7.6e92edd1.js"><link rel="prefetch" href="/assets/js/8.2389170b.js"><link rel="prefetch" href="/assets/js/9.77841f24.js">
    <link rel="stylesheet" href="/assets/css/0.styles.0c36f186.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-1156296a><div data-v-1156296a><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1156296a data-v-1156296a><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-4e82dffc data-v-1156296a data-v-1156296a><h3 class="title" data-v-4e82dffc data-v-4e82dffc>小狐档案库</h3> <p class="description" data-v-4e82dffc data-v-4e82dffc>学习资料</p> <label id="box" class="inputBox" data-v-4e82dffc data-v-4e82dffc><input type="password" value="" data-v-4e82dffc> <span data-v-4e82dffc>Konck! Knock!</span> <button data-v-4e82dffc>OK</button></label> <div class="footer" data-v-4e82dffc data-v-4e82dffc><span data-v-4e82dffc><i class="iconfont reco-theme" data-v-4e82dffc></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-4e82dffc>vuePress-theme-reco</a></span> <span data-v-4e82dffc><i class="iconfont reco-copyright" data-v-4e82dffc></i> <a data-v-4e82dffc><span data-v-4e82dffc>小狐</span>
            
          <span data-v-4e82dffc>2021-9-1 - </span>
          2021
        </a></span></div></div> <div class="hide" data-v-1156296a><header class="navbar" data-v-1156296a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="小狐档案库" class="logo"> <span class="site-name">小狐档案库</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/GO/" class="nav-link"><i class="undefined"></i>
  GO
</a></li><li class="dropdown-item"><!----> <a href="/categories/面试系列/" class="nav-link"><i class="undefined"></i>
  面试系列
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  足迹
</a></div><div class="nav-item"><a href="https://www.xiaohu.team" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-message"></i>
  摇光图库
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1156296a></div> <aside class="sidebar" data-v-1156296a><div class="personal-info-wrapper" data-v-828910c6 data-v-1156296a><img src="/avatar.jpg" alt="author-avatar" class="personal-img" data-v-828910c6> <h3 class="name" data-v-828910c6>
    小狐
  </h3> <div class="num" data-v-828910c6><div data-v-828910c6><h3 data-v-828910c6>58</h3> <h6 data-v-828910c6>Articles</h6></div> <div data-v-828910c6><h3 data-v-828910c6>20</h3> <h6 data-v-828910c6>Tags</h6></div></div> <ul class="social-links" data-v-828910c6></ul> <hr data-v-828910c6></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/GO/" class="nav-link"><i class="undefined"></i>
  GO
</a></li><li class="dropdown-item"><!----> <a href="/categories/面试系列/" class="nav-link"><i class="undefined"></i>
  面试系列
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  足迹
</a></div><div class="nav-item"><a href="https://www.xiaohu.team" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-message"></i>
  摇光图库
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav> <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>go语言学习</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading open"><span>go语言基础学习</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blogs/GO/go/1_go_env.html" class="sidebar-link">Go环境安装配置</a></li><li><a href="/blogs/GO/go/2_go_datatype.html" aria-current="page" class="active sidebar-link">GO基础-基本数据类型</a></li><li><a href="/blogs/GO/go/3_go_var_const.html" class="sidebar-link">Go基础-变量和常量</a></li><li><a href="/blogs/GO/go/5_go_operation.html" class="sidebar-link">GO语言基础之运算符</a></li><li><a href="/blogs/GO/go/24_go_fmt.html" class="sidebar-link">Go语言fmt.Printf使用指南</a></li><li><a href="/blogs/GO/go/25_go_flag.html" class="sidebar-link">Go语言fmt.Printf使用指南</a></li><li><a href="/blogs/GO/go/6_go_array.html" class="sidebar-link">Go语言基础之数组</a></li><li><a href="/blogs/GO/go/7_go_slice.html" class="sidebar-link">Go语言基础之切片</a></li><li><a href="/blogs/GO/go/8_go_map.html" class="sidebar-link">Go语言基础之map</a></li><li><a href="/blogs/GO/go/9_go_pointer.html" class="sidebar-link">Go语言基础之指针</a></li><li><a href="/blogs/GO/go/10_go_func.html" class="sidebar-link">Go语言基础之函数</a></li><li><a href="/blogs/GO/go/11_go_struct.html" class="sidebar-link">Go语言基础之结构体</a></li><li><a href="/blogs/GO/go/12_go_interface.html" class="sidebar-link">Go语言基础之接口</a></li><li><a href="/blogs/GO/go/26_go_reflect.html" class="sidebar-link">Go语言基础之反射</a></li><li><a href="/blogs/GO/go/13_go_package.html" class="sidebar-link">Go语言基础之包(package)</a></li><li><a href="/blogs/GO/go/14_go_channel_lock.html" class="sidebar-link">Go语言中的并发编程</a></li><li><a href="/blogs/GO/go/15_go_socket_udp_tcp.html" class="sidebar-link">Go语言基础之网络编程</a></li><li><a href="/blogs/GO/go/16_go_net_http.html" class="sidebar-link">Go语言基础之net/http介绍</a></li><li><a href="/blogs/GO/go/23_go_Context.html" class="sidebar-link">Go标准库Context</a></li><li><a href="/blogs/GO/go/17_go_test.html" class="sidebar-link">Go语言基础之单元测试</a></li><li><a href="/blogs/GO/go/18_go_mod.html" class="sidebar-link">Go语言基础之依赖管理</a></li><li><a href="/blogs/GO/go/19_go_time.html" class="sidebar-link">Go语言基础之time包</a></li><li><a href="/blogs/GO/go/20_go_strconv.html" class="sidebar-link">Go语言内置包之strconv</a></li><li><a href="/blogs/GO/go/22_go_http_template_1.html" class="sidebar-link">Go语言标准库之http/template</a></li></ul></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>gin</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>数据库操作</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>gorm</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>gorm</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>NoSql</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>消息队列</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>go三方库</span> <span class="arrow right"></span></p> <!----></section></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-4e82dffc data-v-1156296a><h3 class="title" data-v-4e82dffc data-v-4e82dffc>GO基础-基本数据类型</h3> <!----> <label id="box" class="inputBox" data-v-4e82dffc data-v-4e82dffc><input type="password" value="" data-v-4e82dffc> <span data-v-4e82dffc>Konck! Knock!</span> <button data-v-4e82dffc>OK</button></label> <div class="footer" data-v-4e82dffc data-v-4e82dffc><span data-v-4e82dffc><i class="iconfont reco-theme" data-v-4e82dffc></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-4e82dffc>vuePress-theme-reco</a></span> <span data-v-4e82dffc><i class="iconfont reco-copyright" data-v-4e82dffc></i> <a data-v-4e82dffc><span data-v-4e82dffc>小狐</span>
            
          <span data-v-4e82dffc>2021-9-1 - </span>
          2021
        </a></span></div></div> <div data-v-1156296a><main class="page"><section><div class="page-title"><h1 class="title">GO基础-基本数据类型</h1> <div data-v-1ff7123e><i class="iconfont reco-account" data-v-1ff7123e><span data-v-1ff7123e>小狐</span></i> <i class="iconfont reco-date" data-v-1ff7123e><span data-v-1ff7123e>9/1/2021</span></i> <!----> <i class="tags iconfont reco-tag" data-v-1ff7123e><span class="tag-item" data-v-1ff7123e>GO</span></i></div></div> <div class="theme-reco-content content__default"><h1 id="go基础-基本数据类型"><a href="#go基础-基本数据类型" class="header-anchor">#</a> GO基础-基本数据类型</h1> <p>在这里我们要认识Go语言的数据类型</p> <p>在go语言中的数据类型很多如：整形，浮点型，布尔型、字符串外，还有数组、切片、结构体（struct）、函数(一等公民)、map、通道（channel）等。</p> <h3 id="基本数据类型"><a href="#基本数据类型" class="header-anchor">#</a> 基本数据类型</h3> <h4 id="整形"><a href="#整形" class="header-anchor">#</a> 整形</h4> <p>整型分为以下两个大类： 按长度分为：int8、int16、int32、int64 对应的无符号整型：uint8、uint16、uint32、uint64
其中，</p> <table><thead><tr><th style="text-align:center;">类型</th> <th style="text-align:center;">描述</th></tr></thead> <tbody><tr><td style="text-align:center;">uint8</td> <td style="text-align:center;">无符号 8位整型 (0 到 255)</td></tr> <tr><td style="text-align:center;">uint16</td> <td style="text-align:center;">无符号 16位整型 (0 到 65535)</td></tr> <tr><td style="text-align:center;">uint32</td> <td style="text-align:center;">无符号 32位整型 (0 到 4294967295)</td></tr> <tr><td style="text-align:center;">uint64</td> <td style="text-align:center;">无符号 64位整型 (0 到 18446744073709551615)</td></tr> <tr><td style="text-align:center;">int8</td> <td style="text-align:center;">有符号 8位整型 (-128 到 127)</td></tr> <tr><td style="text-align:center;">int16</td> <td style="text-align:center;">有符号 16位整型 (-32768 到 32767)</td></tr> <tr><td style="text-align:center;">int32</td> <td style="text-align:center;">有符号 32位整型 (-2147483648 到 2147483647)</td></tr> <tr><td style="text-align:center;">int64</td> <td style="text-align:center;">有符号 64位整型 (-9223372036854775808 到 9223372036854775807)</td></tr></tbody></table> <h4 id="特殊整型"><a href="#特殊整型" class="header-anchor">#</a> 特殊整型</h4> <table><thead><tr><th style="text-align:center;">类型</th> <th style="text-align:center;">描述</th></tr></thead> <tbody><tr><td style="text-align:center;">uint</td> <td style="text-align:center;">32位操作系统上就是<code>uint32</code>，64位操作系统上就是<code>uint64</code></td></tr> <tr><td style="text-align:center;">int</td> <td style="text-align:center;">32位操作系统上就是<code>int32</code>，64位操作系统上就是<code>int64</code></td></tr> <tr><td style="text-align:center;">uintptr</td> <td style="text-align:center;">无符号整型，用于存放一个指针</td></tr> <tr><td style="text-align:center;">rune</td> <td style="text-align:center;">等同于 int32</td></tr></tbody></table> <p>注意事项：在使用在使用<code>int</code>和 <code>uint</code>类型时，不能假定它是32位或64位的整型，而是考虑<code>int</code>和<code>uint</code>可能在不同平台上的差异。</p> <h4 id="数字字面量"><a href="#数字字面量" class="header-anchor">#</a> 数字字面量</h4> <p>Go1.13版本之后引入了数字字面量语法，这样便于开发者以二进制、八进制或十六进制浮点数的格式定义数字</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//这个例子学过python的小伙伴应该不会很陌生(毕竟不同语言用法不一样，前缀还是一样的)</span>
<span class="token keyword">package</span> main
 
<span class="token keyword">import</span> <span class="token string">&quot;fmt&quot;</span>
 
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">// 默认10进制</span>
	<span class="token keyword">var</span> test1 <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">20</span>

	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%d \n&quot;</span><span class="token punctuation">,</span> test1<span class="token punctuation">)</span>  <span class="token comment">// 20</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%b \n&quot;</span><span class="token punctuation">,</span> test1<span class="token punctuation">)</span>  <span class="token comment">// 10100  占位符%b表示二进制</span>

	<span class="token comment">// 八进制  以0开头</span>
	<span class="token keyword">var</span> test2 <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">020</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%o \n&quot;</span><span class="token punctuation">,</span> test2<span class="token punctuation">)</span>  <span class="token comment">// 20  %o</span>

	<span class="token comment">// 十六进制  以0x开头</span>
	<span class="token keyword">var</span> test3 <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">0xaaa</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%x \n&quot;</span><span class="token punctuation">,</span> test3<span class="token punctuation">)</span>  <span class="token comment">// aaa  %x</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%X \n&quot;</span><span class="token punctuation">,</span> test3<span class="token punctuation">)</span>  <span class="token comment">// AAA  %X</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h4 id="浮点型"><a href="#浮点型" class="header-anchor">#</a> 浮点型</h4> <p>Go语言支持两种浮点型数：<code>float32</code>和 <code>float64</code></p> <p><code>float32</code> 的浮点数的最大范围约为 <code>3.4``e``38</code>，(计算器很大值的时候后面的数字就变成 e了)可以使用常量定义：<code>math.MaxFloat32</code>。 <code>float64</code> 的浮点数的最大范围约为 <code>1.8``e``308</code>，可以使用一个常量定义：<code>math.MaxFloat64</code>。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>package main
import &quot;fmt&quot;

func main() {
 	var test1 float64 = 665.5888
	var test2 float32 = 777.7777

	fmt.Println(test1)
	fmt.Println(test2)
}
//输出
/// 665.5888
/// 777.7777
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h4 id="复数-有"><a href="#复数-有" class="header-anchor">#</a> 复数（有）</h4> <p>complex64和complex128 默认值都是 (0+0i)</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>var c1 complex64
c1 = 1 + 2i
var c2 complex128
c2 = 2 + 3i
fmt.Println(c1)
fmt.Println(c2)
// (1+2i)
// (2+3i)
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>复数有实部和虚部，complex64的实部和虚部为32位(实部和虚部都是 float32)，complex128的实部和虚部为64位(（实部和虚部都是 float64）)。</p> <h4 id="布尔值"><a href="#布尔值" class="header-anchor">#</a> 布尔值</h4> <p>Go语言中以<code>bool</code>类型进行声明布尔型数据，布尔型数据只有<code>true（真）</code>和<code>false（假）</code>两个值。
<strong>注意：</strong></p> <ol><li>布尔类型变量的默认值为<code>false</code>。</li> <li>Go 语言中不允许将整型强制转换为布尔型.</li> <li>布尔型无法参与数值运算，也无法与其他类型进行转换。</li></ol> <h3 id="字符串"><a href="#字符串" class="header-anchor">#</a> 字符串</h3> <p>Go语言中的字符串以原生数据类型出现，使用字符串就像使用其他原生数据类型（int、bool、float32、float64 等）一样。 Go 语言里的字符串的内部实现使用<code>UTF-8</code>编码。 字符串的值为<code>双引号(&quot;)</code>中的内容，可以在Go语言的源码中直接添加非ASCII码字符，例如：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>name <span class="token operator">:=</span> <span class="token string">&quot;小狐&quot;</span>
addr <span class="token operator">:=</span> <span class="token string">&quot;上海&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="字符串转义符"><a href="#字符串转义符" class="header-anchor">#</a> 字符串转义符</h4> <p>Go 语言的字符串常见转义符包含回车、换行、单双引号、制表符等，如下表所示。</p> <table><thead><tr><th style="text-align:center;">转义符</th> <th style="text-align:center;">含义</th></tr></thead> <tbody><tr><td style="text-align:center;"><code>\r</code></td> <td style="text-align:center;">回车符（返回行首）</td></tr> <tr><td style="text-align:center;"><code>\n</code></td> <td style="text-align:center;">换行符（直接跳到下一行的同列位置）</td></tr> <tr><td style="text-align:center;"><code>\t</code></td> <td style="text-align:center;">制表符</td></tr> <tr><td style="text-align:center;"><code>\'</code></td> <td style="text-align:center;">单引号</td></tr> <tr><td style="text-align:center;"><code>\&quot;</code></td> <td style="text-align:center;">双引号</td></tr> <tr><td style="text-align:center;"><code>\\</code></td> <td style="text-align:center;">反斜杠</td></tr></tbody></table> <p><img src="https://cdn.nlark.com/yuque/0/2021/png/611698/1616489438967-2c413681-1767-41b2-aea3-09f4024b665a.png#align=left&amp;display=inline&amp;height=226&amp;margin=%5Bobject%20Object%5D&amp;name=image.png&amp;originHeight=226&amp;originWidth=647&amp;size=10600&amp;status=done&amp;style=none&amp;width=647" alt="image.png"></p> <p>举个例子，我们要打印一个Windows平台下的一个文件路径：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main
<span class="token keyword">import</span> <span class="token punctuation">(</span>
    <span class="token string">&quot;fmt&quot;</span>
<span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">// 转化反斜杠了</span>
  fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;str =  \\ str2  &quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h4 id="多行字符串"><a href="#多行字符串" class="header-anchor">#</a> 多行字符串</h4> <p>Go语言中要定义一个多行字符串时，就必须使用<code>反引号</code>字符：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>line <span class="token operator">:=</span> <span class="token string">`第一行
第二行
第三行
`</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>line<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>反引号间换行将被作为字符串中的换行，但是所有的转义字符均无效，文本将会原样输出。</p> <h4 id="字符串的常用操作"><a href="#字符串的常用操作" class="header-anchor">#</a> 字符串的常用操作</h4> <table><thead><tr><th style="text-align:center;">方法</th> <th style="text-align:center;">介绍</th></tr></thead> <tbody><tr><td style="text-align:center;">len(str)</td> <td style="text-align:center;">求长度</td></tr> <tr><td style="text-align:center;">+或fmt.Sprintf</td> <td style="text-align:center;">拼接字符串</td></tr> <tr><td style="text-align:center;">strings.Split</td> <td style="text-align:center;">分割</td></tr> <tr><td style="text-align:center;">strings.contains</td> <td style="text-align:center;">判断是否包含</td></tr> <tr><td style="text-align:center;">strings.HasPrefix,strings.HasSuffix</td> <td style="text-align:center;">前缀/后缀判断</td></tr> <tr><td style="text-align:center;">strings.Index(),strings.LastIndex()</td> <td style="text-align:center;">子串出现的位置</td></tr> <tr><td style="text-align:center;">strings.EqualFold(s, t string) bool</td> <td style="text-align:center;">判断两个utf-8编码字符串，大小写不敏感</td></tr> <tr><td style="text-align:center;">strings.Contains(s,substr string) bool</td> <td style="text-align:center;">判断字符串s是否包含子串substr</td></tr> <tr><td style="text-align:center;">strings.ContainsAny(s, chars string) bool</td> <td style="text-align:center;">判断字符串s是否包含字符串chars中的任一字符</td></tr> <tr><td style="text-align:center;">strings.Count(s, sep string) int</td> <td style="text-align:center;">返回字符串s中有几个不重复的sep子串</td></tr> <tr><td style="text-align:center;">Index(s, sep string) int</td> <td style="text-align:center;">子串sep在字符串s中第一次出现的位置，不存在则返回-1</td></tr> <tr><td style="text-align:center;">IndexByte(s string, c byte) int</td> <td style="text-align:center;">字符c在s中第一次出现的位置，不存在则返回-1</td></tr> <tr><td style="text-align:center;">IndexAny(s, chars string) int</td> <td style="text-align:center;">字符串chars中的任一utf-8码值在s中第一次出现的位置，如果不存在或者chars为空字符串则返回-1</td></tr> <tr><td style="text-align:center;">IndexFunc(s string, f func(rune) bool) int</td> <td style="text-align:center;">s中第一个满足函数f的位置i（该处的utf-8码值r满足f(r)==true），不存在则返回-1</td></tr> <tr><td style="text-align:center;">Title(s string) string</td> <td style="text-align:center;">返回s中每个单词的首字母都改为标题格式的字符串拷贝</td></tr> <tr><td style="text-align:center;">ToLower(s string) string</td> <td style="text-align:center;">返回将所有字母都转为对应的小写版本的拷贝</td></tr> <tr><td style="text-align:center;">ToUpper(s string) string</td> <td style="text-align:center;">返回将所有字母都转为对应的大写版本的拷贝</td></tr> <tr><td style="text-align:center;">Repeat(s string, count int) string</td> <td style="text-align:center;">返回count个s串联的字符串</td></tr> <tr><td style="text-align:center;">Replace(s, old, new string, n int) string</td> <td style="text-align:center;">返回将s中前n个不重叠old子串都替换为new的新字符串，如果n&lt;0会替换所有old子串</td></tr> <tr><td style="text-align:center;">Map(mapping func(rune) rune, s string) string</td> <td style="text-align:center;">将s的每一个unicode码值r都替换为mapping(r)，返回这些新码值组成的字符串拷贝。如果mapping返回一个负值，将会丢弃该码值而不会被替换</td></tr> <tr><td style="text-align:center;">Trim(s string, cutset string) string</td> <td style="text-align:center;">返回将s前后端所有cutset包含的utf-8码值都去掉的字符串</td></tr> <tr><td style="text-align:center;">TrimSpace(s string) string</td> <td style="text-align:center;">返回将s前后端所有空白（unicode.IsSpace指定）都去掉的字符串</td></tr> <tr><td style="text-align:center;">TrimFunc(s string, f func(rune) bool) string</td> <td style="text-align:center;">返回将s前后端所有满足f的unicode码值都去掉的字符串</td></tr> <tr><td style="text-align:center;">strings.Join(a[]string, sep string)</td> <td style="text-align:center;">join操作</td></tr></tbody></table> <h3 id=""><a href="#" class="header-anchor">#</a></h3> <p>组成每个字符串的元素叫做“字符”，可以通过遍历或者单个获取字符串元素获得字符。 字符用单引号（’）包裹起来，如：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code> a <span class="token operator">:=</span> <span class="token string">'中'</span>
 b <span class="token operator">:=</span> <span class="token string">'x'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>Go 语言的字符有以下两种：</p> <ol><li><code>uint8</code>类型，或者叫 byte 型，代表了<code>ASCII码</code>的一个字符。</li> <li><code>rune</code>类型，代表一个 <code>UTF-8字符</code>。</li></ol> <p>当需要处理中文、日文或者其他复合字符时，则需要用到<code>rune</code>类型。<code>rune</code>类型实际是一个<code>int32</code>。
Go 使用了特殊的 rune 类型来处理 Unicode，让基于 Unicode 的文本处理更为方便，也可以使用 byte 型进行默认字符串处理，性能和扩展性都有照顾。</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 遍历字符串</span>
<span class="token keyword">func</span> <span class="token function">traversalString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">//不能单独使用 如： var a rune = '1' //会报错 rune==int32  byte也不能这样声明</span>
    <span class="token comment">// fmt.Printf 的占位符这些在后面的fmt包讲解</span>
   
	s <span class="token operator">:=</span> <span class="token string">&quot;xiaohu小狐&quot;</span>
	<span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token function">len</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</span> <span class="token comment">//byte</span>
		fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%v(%c) &quot;</span><span class="token punctuation">,</span> s<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> s<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> r <span class="token operator">:=</span> <span class="token keyword">range</span> s <span class="token punctuation">{</span> <span class="token comment">//rune</span>
        
		fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%v(%c) &quot;</span><span class="token punctuation">,</span> r<span class="token punctuation">,</span> r<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment">//这一段细心看</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p>输出：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>120(x) 105(i) 97(a) 111(o) 104(h) 117(u) 229(å) 176(°) 143( ) 231(ç) 139( ) 144( ) 
120(x) 105(i) 97(a) 111(o) 104(h) 117(u) 23567(小) 29392(狐) 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>因为UTF8编码下一个中文汉字由3~4个字节组成，所以我们不能简单的按照字节去遍历一个包含中文的字符串，否则就会出现上面输出中第一行的结果。
字符串底层是一个byte数组，所以可以和<code>[]byte</code>类型相互转换。字符串是不能修改的 字符串是由byte字节组成，所以字符串的长度是byte字节的长度。 rune类型用来表示utf8字符，一个rune字符由一个或多个byte组成。</p> <h3 id="修改字符串"><a href="#修改字符串" class="header-anchor">#</a> 修改字符串</h3> <p>要修改字符串，需要先将其转换成<code>[]rune</code>或<code>[]byte</code>，完成后再转换为<code>string</code>。无论哪种转换，都会重新分配内存，并复制字节数组。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>func changeString() {
	s1 := &quot;big&quot;
	// 强制类型转换
	byteS1 := []byte(s1)
	byteS1[0] = 'p'
	fmt.Println(string(byteS1))
	s2 := &quot;白萝卜&quot;
	runeS2 := []rune(s2)
	runeS2[0] = '红'
	fmt.Println(string(runeS2))
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h3 id="类型转换"><a href="#类型转换" class="header-anchor">#</a> 类型转换</h3> <p>Go语言中只有强制类型转换，没有隐式类型转换。该语法只能在两个类型之间支持相互转换的时候使用。
强制类型转换的基本语法如下：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>T(表达式)
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>其中，T表示要转换的类型。表达式包括变量、复杂算子和函数返回值等.
比如计算直角三角形的斜边长时使用math包的Sqrt()函数，该函数接收的是float64类型的参数，而变量a和b都是int类型的，这个时候就需要将a和b强制类型转换为float64类型。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>func sqrtDemo() {
	var a, b = 3, 4
	var c int
	// math.Sqrt()接收的参数是float64类型，需要强制转换
	c = int(math.Sqrt(float64(a*a + b*b)))
	fmt.Println(c)
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">2021-9-2: </span> <span class="time">9/3/2021, 1:12:02 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/blogs/GO/go/1_go_env.html" class="prev">
            Go环境安装配置
          </a></span> <span class="next"><a href="/blogs/GO/go/3_go_var_const.html">
            Go基础-变量和常量
          </a></span></p></div> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-70334359><li class="level-3" data-v-70334359><a href="/blogs/GO/go/2_go_datatype.html#基本数据类型" class="sidebar-link reco-side-基本数据类型" data-v-70334359>基本数据类型</a></li><li class="level-3" data-v-70334359><a href="/blogs/GO/go/2_go_datatype.html#字符串" class="sidebar-link reco-side-字符串" data-v-70334359>字符串</a></li><li class="level-3" data-v-70334359><a href="/blogs/GO/go/2_go_datatype.html#" class="sidebar-link reco-side-" data-v-70334359></a></li><li class="level-3" data-v-70334359><a href="/blogs/GO/go/2_go_datatype.html#修改字符串" class="sidebar-link reco-side-修改字符串" data-v-70334359>修改字符串</a></li><li class="level-3" data-v-70334359><a href="/blogs/GO/go/2_go_datatype.html#类型转换" class="sidebar-link reco-side-类型转换" data-v-70334359>类型转换</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><!----><canvas id="vuepress-canvas-cursor"></canvas></div></div>
    <script src="/assets/js/app.7029ddab.js" defer></script><script src="/assets/js/3.ebaa3085.js" defer></script><script src="/assets/js/1.8ce67e8c.js" defer></script><script src="/assets/js/29.4d95742d.js" defer></script>
  </body>
</html>
